package com.training.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.training.common.Result;
import com.training.common.TrainingPage;
import com.training.common.model.Class;
import com.training.common.model.Student;
import com.training.common.model.User;
import com.training.service.ClassService;
import com.training.service.ClassServiceImp;
import com.training.service.StudentService;
import com.training.service.StudentServiceImp;

import java.util.*;

public class StudentController extends Controller {

    @Inject(StudentServiceImp.class)
    private StudentService studentService;

    @Inject(ClassServiceImp.class)
    private ClassService classService;

    public void index(){
        List<Class> classList = classService.getClassList();
        setAttr("classList",classList);
        render("/views/student/index.html");
    }

    public void getStudentPage(){
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String name = getPara("name");
        String number = getPara("number");
        String class_id = getPara("class_id");
        Page<Student> studentPage = studentService.getStudentPage(pageNumber,pageSize,name,number,class_id);
        Result result = new Result();
        result.setData(new TrainingPage(studentPage));
        renderJson(result);
    }

    public void showAddStudent(){
        List<Class> classList = classService.getClassList();
        setAttr("classList",classList);
        render("/views/student/add.html");
    }

    public void addStudent(){
        String number = getPara("number");
        String name = getPara("name");
        int sex = getParaToInt("sex");
        int age = getParaToInt("age");
        int class_id = getParaToInt("class_id");
        User user = getSessionAttr("trainingsession");
        Student student = new Student();
        student.setNumber(number);
        student.setName(name);
        student.setSex(sex);
        student.setAge(age);
        student.setClassId(class_id);
        student.setUserId(user.getID());
        student.setCreatetime(new Date());
        boolean boo = student.save();
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("添加成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("添加失败！");
        }
        renderJson(result);
    }

    @Before(Tx.class)
    public void deleteStudent(){
        String ids = getPara("id");
        String[] idss = ids.split(",");
        Result result = new Result();
        boolean boo = false;
        for (String id : idss){
            int idi = Integer.parseInt(id);
            boo = Student.dao.deleteById(idi);
        }
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("删除成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败！");
        }
        renderJson(result);
    }

    public void showEditStudent(){
        String id = getPara("id");
        setAttr("id",id);
        List<Class> classList = classService.getClassList();
        setAttr("classList",classList);
        render("/views/student/edit.html");
    }

    public void getStudent(){
        String id = getPara("id");
        Student student = Student.dao.findById(id);
        Result result = new Result();
        result.setData(student);
        renderJson(result);
    }

    public void editStudent(){
        String id = getPara("id");
        String number = getPara("number");
        String name = getPara("name");
        int sex = getParaToInt("sex");
        int age = getParaToInt("age");
        int class_id = getParaToInt("class_id");
        Result result = new Result();
        Student student = Student.dao.findById(id);
        student.setNumber(number);
        student.setName(name);
        student.setSex(sex);
        student.setAge(age);
        student.setClassId(class_id);
        boolean boo = student.update();
        if (boo) {
            result.setErrorNo(0);
            result.setErrorInfo("更新成功！");
        } else {
            result.setErrorNo(500);
            result.setErrorInfo("更新失败！");
        }
        renderJson(result);
    }

    public void getStudentListTrainid(){
        String trainid = getPara("trainid");
        String number = getPara("number");
        List<Student> studentList = studentService.getStudentListTrainid(trainid,number);
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("code",0);
        dataMap.put("msg","success");
        List<Map<String,Object>> data = new ArrayList<>();
        Map<String,Object> map = null;
        for(Student student : studentList){
            map = new HashMap<String,Object>();
            map.put("name",student.getName());
            map.put("value",student.getId());
            data.add(map);
        }
        dataMap.put("data",data);
        renderJson(dataMap);
    }
}
